package com.yshebkb.sulp.dao;

import com.yshebkb.sulp.entity.Role;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author yshebkb
 */
@Repository
@Mapper
public interface RoleMapper {
    @Delete({
            "delete from role",
            "where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
            "insert into role (id, name, ",
            "description, status, ",
            "creator, create_time, ",
            "update_time)",
            "values (#{id,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR}, ",
            "#{description,jdbcType=VARCHAR}, #{status,jdbcType=TINYINT}, ",
            "#{creator,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, ",
            "#{updateTime,jdbcType=TIMESTAMP})"
    })
    int insert(Role record);

    int insertSelective(Role record);

    @Select("select * from role where id = #{id}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "users", column = "id",
                    many = @Many(select = "com.yshebkb.sulp.dao.UserMapper.selectAllUsers",
                            fetchType = FetchType.LAZY)),
            @Result(property = "menus", column = "id",
                    many = @Many(select = "com.yshebkb.sulp.dao.MenuMapper.selectAllMenus",
                            fetchType = FetchType.LAZY))
    })
    Role selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(Role record);

    @Update({
            "update role",
            "set name = #{name,jdbcType=VARCHAR},",
            "description = #{description,jdbcType=VARCHAR},",
            "status = #{status,jdbcType=TINYINT},",
            "creator = #{creator,jdbcType=VARCHAR},",
            "create_time = #{createTime,jdbcType=TIMESTAMP},",
            "update_time = #{updateTime,jdbcType=TIMESTAMP}",
            "where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(Role record);

    @Select("select * from role")
    List<Role> findAll();

    @Select("select * from role where id in (select user_role.role_id from user_role where user_role.user_id=#{id})")
    List<Role> selectAllRoles(Long id);
}